import json
import h5py

gqa_objects_features = '../data_gqa/gqa_objects.h5'
gqa_merged_info = '../data_gqa/gqa_objects_merged_info.json'



# def load_folder(folder):
#     if folder.endswith('json'):
#         return json.load(open(folder, 'r'))
#
#     if folder.endswith('hdf5'):
#         return h5py.File(folder, 'r')

def gqa_megerd_info_imageId_loader(imageId):
    img_info = json.load(open(gqa_merged_info, 'r'))
    return img_info[imageId]


def gqa_objects_feature_loader(imageId):
    image_Id = gqa_megerd_info_imageId_loader(imageId)
    h = h5py.File(gqa_objects_features, 'r')
    image_features = h['features'][image_Id['idx'], 0:36]
    return image_features


def gqa_merged_info_size_loader(imageId):
    image_Id = gqa_megerd_info_imageId_loader(imageId)
    image_width = image_Id['width']
    image_height = image_Id['height']
    return image_width, image_height

def gqa_objects_bboxes_loader(imageId):
    image_Id = gqa_megerd_info_imageId_loader(imageId)
    h = h5py.File(gqa_objects_features, 'r')
    bboxes = h['bboxes'][image_Id['idx'], 0:36]
    return bboxes

def gqa_objects_coordinate_loader(imageId):
    image_Id = gqa_megerd_info_imageId_loader(imageId)
    h = h5py.File(gqa_objects_features)
    bboxes = h['bboxes'][image_Id['idx'], 0: 37]
    coordinator = bboxes[:,0:2]
    return coordinator
    pass


